package com.liyghting.datastructurealgorithm.array;

/**
 * @author LiYiGuang
 * @date 2020/1/8 15:22
 */
public class Array<T> {
    /**
     * 数组元素
     */
    private T[] data;

    /**
     * 数组中的元素个数
     */
    private int count;

    public Array() {
        this(10);
    }

    public Array(int capacity) {
        this.data = (T[])new Object[capacity];

        this.count = 0;
    }

    public void add(T e) {
        if (count == data.length) {
            resize(data.length * 2);
        }
        data[count++] = e;
    }

    public void resize(int capacity) {
        T[] newData = (T[])new Object[capacity];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
    }
}
